/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package logica;

import org.jvnet.substance.SubstanceLookAndFeel;

import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import javax.swing.*;
import vista.Principal;

/**
 *
 * @author Administrador
 */
public class SystraySira {

    private final Principal p;

    public SystraySira(Principal p) {
        this.p = p;
    }

    public void asignaIConoEncabezado() {
        p.setIconImage(new ImageIcon(getClass().getResource("/img/sira-gif.GIF")).getImage());
    }

    public void lookAndFeel() {
//        JFrame frame = (JFrame) p;
        JFrame.setDefaultLookAndFeelDecorated(true); //que nos permite dejar a Substance la decoracion ( por asi decirlo) 
        SubstanceLookAndFeel.setSkin("org.jvnet.substance.skin.BusinessBlackSteelSkin"); //
    }

    public void createAndShowGUI() {
        //Check the SystemTray support
        if (!SystemTray.isSupported()) {
            System.out.println("SystemTray is not supported");
            return;
        }
        final PopupMenu popup = new PopupMenu();
        final TrayIcon trayIcon = new TrayIcon(createImage("/img/sira-gif2.GIF", "SIRA"));
        final SystemTray tray = SystemTray.getSystemTray();
        trayIcon.setToolTip("Systema de Riego Automatico SIRA");         

        // Create a popup menu components
        MenuItem openItem = new MenuItem("Abrir");
        MenuItem aboutItem = new MenuItem("About");
        MenuItem exitItem = new MenuItem("Exit");

        popup.add(openItem);
        popup.addSeparator();
        popup.add(aboutItem);
        popup.addSeparator();
        popup.add(exitItem);

        trayIcon.setPopupMenu(popup);

        try {
            tray.add(trayIcon);
        } catch (AWTException e) {
            System.out.println("TrayIcon could not be added.");
            return;
        }

        trayIcon.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (p.isVisible()) {
                    p.dispose();
                } else {
                    p.setVisible(true);
                }
            }
        });
        
         openItem.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(!p.isVisible()){
                    p.setVisible(true);
                }
            }
        });

        aboutItem.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null, "Sistema de Riego Automatico SIRA Inifap");
            }
        });

        exitItem.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                tray.remove(trayIcon);
                System.exit(0);
            }
        });
    }

    protected static Image createImage(String path, String description) {
        URL imageURL = SystraySira.class.getResource(path);

        if (imageURL == null) {
            System.err.println("Resource not found: " + path);
            return null;
        } else {
            return (new ImageIcon(imageURL, description)).getImage();
        }
    }
}
